package com.example.his.api.front.impl;

import cn.hutool.core.util.RandomUtil;
import com.example.his.api.db.dao.CustomerDao;
import com.example.his.api.db.dao.OrderDao;
import com.example.his.api.db.pojo.CustomerEntity;
import com.example.his.api.front.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service ( "FrontCustomerServiceImpl" )
@Slf4j
public class CustomerServiceImpl implements CustomerService {

  @Resource
  private RedisTemplate redisTemplate;

  @Resource
  private CustomerDao customerDao;

  @Resource
  private OrderDao orderDao;

  /**
   * @param tel
   * @return
   */
  @Override
  public boolean sendSmsCode( String tel ) {

    //生成随机的6位数字
    String code = RandomUtil.randomNumbers( 6 );
    log.info( "生成验证码：{}", code );

    String key = "sms_code_refresh" + tel;

    //判断是否禁止向某个电话号码发送短信验证码
    if ( redisTemplate.hasKey( key ) ) {
      return false;
    }

    // 创建禁止重新发送验证码
    redisTemplate.opsForValue().set( key, code );
    redisTemplate.expire( key, 5, TimeUnit.MINUTES );

    // 把数字和电话缓存到redis中并且设置过期时间
    key = "sms_code" + tel;
    redisTemplate.opsForValue().set( key, code );
    redisTemplate.expire( key, 5, TimeUnit.MINUTES );

    return true;
  }

  /**
   * @param tel
   * @param code
   * @return
   */
  @Override
  @Transactional
  public HashMap login( String tel, String code ) {

    // 创建HasMap对象
    HashMap map = new HashMap();

    // 核对短信验证码和电话号码
    String key = "sms_code" + tel;
    if ( !redisTemplate.hasKey( key ) ) {
      map.put( "result", false );
      map.put( "msg", "验证码已过期" );
      return map;
    }

    String cacheCode = redisTemplate.opsForValue().get( key ).toString();
    if ( !cacheCode.equals( code ) ) {
      map.put( "result", false );
      map.put( "msg", "短信验证码错误" );
      return map;
    }

    redisTemplate.delete( key );
    key = "sms_code_refresh" + tel;
    redisTemplate.delete( key );

    Integer id = customerDao.searchIdByTel( tel );
    //判断是不是新用户
    if ( id == null ) {
      CustomerEntity entity = new CustomerEntity();
      entity.setTel( tel );
      //注册新用户
      customerDao.insert( entity );
      id = entity.getId();
    }
    map.put( "id", id );
    map.put( "result", true );
    map.put( "msg", "登录成功" );

    return map;
  }

  /**
   * 查询顾客前端统计信息
   *
   * @param customerId 顾客ID
   * @return 包含顾客信息和订单统计的HashMap
   */
  @Override
  public HashMap searchSummary( int customerId ) {

    // 查询顾客基本信息
    HashMap map = customerDao.searchById( customerId );
    // 查询顾客订单统计信息并合并到结果中
    map.putAll( orderDao.searchFrontStatistic( customerId ) );

    return map;
  }

  /**
   * 更新顾客信息
   *
   * @param param 包含更新信息的参数Map
   * @return 更新成功返回true，否则返回false
   */
  @Override
  @Transactional
  public boolean update( Map param ) {

    // 调用DAO层执行更新操作，并获取影响的行数
    int rows = customerDao.update( param );

    // 根据影响行数判断更新是否成功（影响1行表示成功）
    return rows == 1;
  }
}
